/*
 * $Id: ActTask.java,v 1.35 2009/12/16 18:31:22 navdeep_mahajan Exp $
 *
 * Copyright (C) 2006 General Electric Company. All Rights Reserved.
 *
 * This software is the confidential and proprietary information of the General
 * Electric Company (GE). You shall not disclose this software and shall use it
 * only in accordance with the terms of the license agreement you entered into
 * with GE.
 *
 * GE MAKES NO REPRESENTATIONS OR WARRANTIES ABOUT THE SUITABILITY OF THE
 * SOFTWARE, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE IMPLIED
 * WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, OR
 * NON-INFRINGEMENT. GE SHALL NOT BE LIABLE FOR ANY DAMAGES SUFFERED BY LICENSEE
 * AS A RESULT OF USING, MODIFYING, OR DISTRIBUTING THIS SOFTWARE OR ITS
 * DERIVATIVES.
 */

package com.ge.healthcare.autosc.common.database.pojo;

import java.util.Date;

import com.ge.healthcare.autosc.common.ASCUtil;
import com.ge.healthcare.autosc.common.database.dao.ICodeDetailsDAO;
import com.ge.healthcare.autosc.common.dbLogger.StepLogger;
import com.ge.healthcare.autosc.common.util.TimeFormatStandard;

/**
 * ActTask entity.
 * 
 * @author MyEclipse Persistence Tools
 */
public class ActTask extends AbstractTask implements java.io.Serializable,
		IActTask {
	/**
	 * Eclipse generated
	 */
	private static final long serialVersionUID = 3594402120250651677L;
	public static final String TABLE_NAME = "com.ge.healthcare.autosc.common.database.pojo.ActTask";
	public static final String COL_TASK_ID = "taskId";
	public static final String COL_REQUEST_ID = "requestId";
	public static final String COL_TASK_TYPE = "taskType";
	public static final String COL_PRIORITY = "priority";
	public static final String COL_CODE_ID = "codeId";
	public static final String COL_STEP_TYPE_ID = "stepTypeId";

	public Date queuedTime = null;

	/** default constructor */
	public ActTask() {
		this.setStepTypeId(StepLogger.getStepTypeId());
	}

	public ActTask(ActRequest request, CfgTaskType taskType) {
		this.setStepTypeId(StepLogger.getStepTypeId());
		this.setRequestId(request.getRequestId());
		this.setTaskType(taskType.getTaskTypeName());
	}

	/**
	 * @param task
	 * @return
	 */
	public boolean equals(ActTask task) {
		if (this.getTaskId() != null
				&& this.getTaskId().equals(task.getTaskId())) {
			return true;
		} else if (this.getRequestId() != null
				&& this.getRequestId().equals(task.getRequestId())
				&& this.getTaskType() != null
				&& this.getTaskType().equals(task.getTaskType())) {
			return true;
		}
		return false;
	}

	/**
	 * @see java.lang.Object#toString()
	 */
	public String toString() {
		StringBuffer sb = new StringBuffer();
		sb.append("{Task Id=").append(this.getTaskId()).append("},");
		sb.append("{Task Type=").append(this.getTaskType()).append("},");
		sb.append("{Parent Id=").append(this.getParentId()).append("},");
		sb.append("{Request Id=").append(this.getRequestId()).append("},");
		sb.append("{Failed Count=").append(this.getFailedCount()).append("},");
		sb.append("{stepTypeId=").append(this.getStepTypeId()).append("},");
		sb.append("{Code Id=").append(this.getCodeId()).append("},");
		sb.append("{Ex AutoSC=").append(this.getExtAutosc()).append("},");
		sb.append("{Priority=").append(this.getPriority()).append("},");
		sb.append("{Retry Until=");
		sb.append(formateDate(this.getRetryUntil())).append("},");
		sb.append("{Expire Date =").append(formateDate(this.getExpireDate()))
				.append("},");
		sb.append("{Start Time=");
		sb.append(formateDate(this.getStartTime())).append("},");
		sb.append("{End Time=");
		sb.append(formateDate(this.getEndTime())).append("},");
		sb.append("{Queued Time=");
		sb.append(formateDate(this.getQueuedTime())).append("}");
		return sb.toString();
	}

	private StringBuffer formateDate(Date date) {
		StringBuffer sb = new StringBuffer();
		if (!ASCUtil.isNullOrEmptyObject(date)) {
			sb.append(TimeFormatStandard.getDtInEndUserDisplayFormat(date));
		} else {
			sb.append("Null");
		}
		return sb;
	}

	public void setRequestInfo(ActRequest request) {
		this.setRequestId(request.getRequestId());
	}

	public void incrementFailedCount() {
		this.setFailedCount(this.getFailedCount() + 1);
	}

	public boolean isExternal() {
		if (this.getExtAutosc() == null
				|| this.getExtAutosc().equals(CfgTaskType.EXTERNAL_AUTOSC)) {
			return true;
		}
		return false;
	}

	public Long getObjectId() {
		return this.getTaskId();
	}

	public String getObjectType() {
		return HstStepTime.TASK_TYPE;
	}

	public void setTaskCode(ICodeDetailsDAO iCodeDetailsDAO, String codeName) {
		CfgCodeDetails codeDetails = (CfgCodeDetails) iCodeDetailsDAO
				.getUniqueCodedetailsByCodeName(codeName);
		if (codeDetails != null) {
			setCodeId(codeDetails.getCodeId());
		} else {
			throw new RuntimeException(
					"Unable to set task code because code is not found "
							+ "with code name " + codeName);
		}
	}

	public boolean hasTaskCode(ICodeDetailsDAO iCodeDetailsDAO, String codeName) {
		boolean match = false;
		CfgCodeDetails codeDetails = (CfgCodeDetails) iCodeDetailsDAO
				.getUniqueCodedetailsByCodeName(codeName);
		if (codeDetails != null) {
			match = this.getCodeId().equals(codeDetails.getCodeId());
		}
		return match;
	}

	public void setEndTime() {
		this.setEndTime(TimeFormatStandard.getNowMillis());
	}

	public void setStartTime() {
		this.setStartTime(TimeFormatStandard.getNowMillis());

	}

	public void setExpireDate() {
		this.setExpireDate(TimeFormatStandard.getNowMillis());
	}

	public void setPriority(Long priority) {
		if (ASCUtil.isNullOrEmptyObject(priority)) {
			throw new RuntimeException("Priority May not have a Null Value");
		} else if (priority > 5L || priority < 1L) {
			throw new RuntimeException(
					"Priority May not have greater then 5 or less then 1");
		}
		super.setPriority(priority);
	}

	public void setExternalAutoSC() {
		super.setExtAutosc(CfgTaskType.EXTERNAL_AUTOSC);
	}

	public void setInternalAutoSC() {
		super.setExtAutosc(CfgTaskType.INTERNAL_AUTOSC);
	}

	public boolean isValid() {
		if (ASCUtil.isNullOrEmptyObject(this.getTaskId())
				|| ASCUtil.isNullOrEmptyObject(this.getRequestId())
				|| ASCUtil.isNullOrEmptyObject(this.getFailedCount())
				|| ASCUtil.isNullOrEmptyObject(this.getStepTypeId())
				|| ASCUtil.isNullOrEmptyObject(this.getTaskType())
				|| ASCUtil.isNullOrEmptyObject(this.getExtAutosc())
				|| ASCUtil.isNullOrEmptyObject(this.getPriority())
				|| ASCUtil.isNullOrEmptyObject(this.getExpireDate())) {
			return false;
		}
		return true;
	}

	public boolean isClosed() {
		if (ASCUtil.isNullOrEmptyObject(this.getEndTime())) {
			return false;
		} else {
			return true;
		}
	}

	/**
	 * Check that task has a TaskId
	 * 
	 * @return
	 */
	public boolean isValidId() {
		boolean isValid = true;
		if (ASCUtil.isNullOrEmptyObject(this.getTaskId())) {
			isValid = false;
		}
		return isValid;
	}

	public boolean isTask() {
		return true;
	}

	public ActTask copy() {
		ActTask task = new ActTask();
		if (!ASCUtil.isNullOrEmptyObject(this.getTaskId())) {
			task.setTaskId(new Long(this.getTaskId()));
		}
		if (!ASCUtil.isNullOrEmptyObject(this.getRequestId())) {
			task.setRequestId(new Long(this.getRequestId()));
		}
		if (!ASCUtil.isNullOrEmptyObject(this.getFailedCount())) {
			task.setFailedCount(new Long(this.getFailedCount()));
		}
		if (!ASCUtil.isNullOrEmptyObject(this.getParentId())) {
			task.setParentId(new Long(this.getParentId()));
		}
		if (!ASCUtil.isNullOrEmptyObject(this.getStepTypeId())) {
			task.setStepTypeId(new Long(this.getStepTypeId()));
		}
		if (!ASCUtil.isNullOrEmptyObject(this.getCodeId())) {
			task.setCodeId(new Long(this.getCodeId()));
		}
		if (!ASCUtil.isNullOrEmptyObject(this.getTaskType())) {
			task.setTaskType(new String(this.getTaskType()));
		}
		if (!ASCUtil.isNullOrEmptyObject(this.getExtAutosc())) {
			task.setExtAutosc(this.getExtAutosc());
		}
		if (!ASCUtil.isNullOrEmptyObject(this.getStartTime())) {
			task.setStartTime(new Date(this.getStartTime().getTime()));
		}
		if (!ASCUtil.isNullOrEmptyObject(this.getEndTime())) {
			task.setEndTime(new Date(this.getEndTime().getTime()));
		}
		if (!ASCUtil.isNullOrEmptyObject(this.getPriority())) {
			task.setPriority(new Long(this.getPriority()));
		}
		if (!ASCUtil.isNullOrEmptyObject(this.getExpireDate())) {
			task.setExpireDate(new Date(this.getExpireDate().getTime()));
		}
		if (!ASCUtil.isNullOrEmptyObject(this.getRetryUntil())) {
			task.setRetryUntil(new Date(this.getRetryUntil().getTime()));
		}
		if (!ASCUtil.isNullOrEmptyObject(this.getQueuedTime())) {
			task.setQueuedTime(this.getQueuedTime());
		}
		return task;
	}

	/**
	 * @return the queuedTime
	 */
	public Date getQueuedTime() {
		return queuedTime;
	}

	/**
	 * @param queuedTime
	 *            the queuedTime to set
	 */
	public void setQueuedTime(Date queuedTime) {
		this.queuedTime = queuedTime;
	}
}
